package lily.phs.urh;

import lily.phs.object.HealthTest;
import lily.phs.urh.R;
import lily.ui.control.MyPopupWindow;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * 健康体检表（Page 4）的界面布局和数据处理封装的view类
 * @author heckassitant
 *
 */
public class HealthTest_4 extends LinearLayout implements OnClickListener, OnFocusChangeListener{

//	private MyPopupWindow mPopupWindow;
	private Context mContext;
	private int profileid = 0;
	private int testid = 0;
	
	private EditText et_serum_cpt,et_serum_ast,et_albumin,et_tbil,et_dbil,et_serum_creatinine,et_bun,et_blood_k_chroma,et_blood_na_chroma,
	                 et_tc,et_trig,et_serum_ldlc,et_serum_hdlc,et_chest_x,et_b_mode,et_cervical_smear,et_assitest_other;
	
	private RadioGroup rgHbsab,rgChestX,rgBMode,rgCervicalSmear,rgQualityPinghe,rgQualityQixu,rgQualityYangxu,
	                   rgQualityYinxu,rgQualityTanshi,rgQualityShire,rgQualityXueyu,rgQualityQiyu,rgQualityTebing;
	private RadioButton rb_hbsab_1,rb_hbsab_2,rb_chest_x_1,rb_chest_x_2,rb_b_mode_1,rb_b_mode_2,rb_cervical_smear_1,rb_cervical_smear_2,
	                    rb_quality_pinghe_1,rb_quality_pinghe_2,rb_quality_qixu_1,rb_quality_qixu_2,rb_quality_yangxu_1,rb_quality_yangxu_2,
	                    rb_quality_yinxu_1,rb_quality_yinxu_2,rb_quality_tanshi_1,rb_quality_tanshi_2,rb_quality_shire_1,rb_quality_shire_2,
	                    rb_quality_xueyu_1,rb_quality_xueyu_2,rb_quality_qiyu_1,rb_quality_qiyu_2,rb_quality_tebing_1,rb_quality_tebing_2;
	
	private HealthTest ht;
	private View child;
	
	public HealthTest_4(Context context,int profileId,int testId) {
		super(context);
		// TODO Auto-generated constructor stub
		//档案编号
		this.profileid = profileId;
		this.testid = testId;
		this.mContext = context;
		
		final LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		child = inflater.inflate(R.layout.healthtest_4, null);
	}
	
	public int getProfileid() {
		return profileid;
	}

	public void setProfileid(int profileid) {
		this.profileid = profileid;
	}

	public int getTestid() {
		return testid;
	}

	public void setTestid(int testid) {
		this.testid = testid;
	}

	public void findViewsById() {
		// TODO Auto-generated method stub
		
		et_serum_cpt = (EditText)child.findViewById(R.id.ht4_serum_cpt);
		et_serum_ast = (EditText)child.findViewById(R.id.ht4_serum_ast);
		et_albumin = (EditText)child.findViewById(R.id.ht4_albumin);
		et_tbil = (EditText)child.findViewById(R.id.ht4_tbil);
		et_dbil = (EditText)child.findViewById(R.id.ht4_dbil);
		et_serum_creatinine = (EditText)child.findViewById(R.id.ht4_serum_creatinine);
		et_bun = (EditText)child.findViewById(R.id.ht4_bun);
		et_blood_k_chroma = (EditText)child.findViewById(R.id.ht4_blood_k_chroma);
		et_blood_na_chroma = (EditText)child.findViewById(R.id.ht4_blood_na_chroma);
        et_tc = (EditText)child.findViewById(R.id.ht4_tc);
        et_trig = (EditText)child.findViewById(R.id.ht4_trig);
        et_serum_ldlc = (EditText)child.findViewById(R.id.ht4_serum_ldlc);
        et_serum_hdlc = (EditText)child.findViewById(R.id.ht4_serum_hdlc);
        et_chest_x = (EditText)child.findViewById(R.id.ht4_chest_x);
        et_chest_x.setOnFocusChangeListener(this);
        et_chest_x.setOnClickListener(this);
        et_b_mode = (EditText)child.findViewById(R.id.ht4_b_mode);
        et_b_mode.setOnFocusChangeListener(this);
        et_b_mode.setOnClickListener(this);
        et_cervical_smear = (EditText)child.findViewById(R.id.ht4_cervical_smear);
        et_cervical_smear.setOnFocusChangeListener(this);
        et_cervical_smear.setOnClickListener(this);
        et_assitest_other = (EditText)child.findViewById(R.id.ht4_assitest_other);
		rb_hbsab_1 = (RadioButton)child.findViewById(R.id.ht4_hbsab_1);
		rb_hbsab_2 = (RadioButton)child.findViewById(R.id.ht4_hbsab_2);
		rb_chest_x_1 = (RadioButton)child.findViewById(R.id.ht4_chest_x_1);
		rb_chest_x_2 = (RadioButton)child.findViewById(R.id.ht4_chest_x_2);
		rb_b_mode_1 = (RadioButton)child.findViewById(R.id.ht4_b_mode_1);
		rb_b_mode_2 = (RadioButton)child.findViewById(R.id.ht4_b_mode_2);
		rb_cervical_smear_1 = (RadioButton)child.findViewById(R.id.ht4_cervical_smear_1);
		rb_cervical_smear_2 = (RadioButton)child.findViewById(R.id.ht4_cervical_smear_2);
        rb_quality_pinghe_1 = (RadioButton)child.findViewById(R.id.ht4_quality_pinghe_1);
        rb_quality_pinghe_2 = (RadioButton)child.findViewById(R.id.ht4_quality_pinghe_2);
        rb_quality_qixu_1 = (RadioButton)child.findViewById(R.id.ht4_quality_qixu_1);
        rb_quality_qixu_2 = (RadioButton)child.findViewById(R.id.ht4_quality_qixu_2);
        rb_quality_yangxu_1 = (RadioButton)child.findViewById(R.id.ht4_quality_yangxu_1);
        rb_quality_yangxu_2 = (RadioButton)child.findViewById(R.id.ht4_quality_yangxu_2);
        rb_quality_yinxu_1 = (RadioButton)child.findViewById(R.id.ht4_quality_yinxu_1);
        rb_quality_yinxu_2 = (RadioButton)child.findViewById(R.id.ht4_quality_yinxu_2);
        rb_quality_tanshi_1 = (RadioButton)child.findViewById(R.id.ht4_quality_tanshi_1);
        rb_quality_tanshi_2 = (RadioButton)child.findViewById(R.id.ht4_quality_tanshi_2);
        rb_quality_shire_1 = (RadioButton)child.findViewById(R.id.ht4_quality_shire_1);
        rb_quality_shire_2 = (RadioButton)child.findViewById(R.id.ht4_quality_shire_2);
        rb_quality_xueyu_1 = (RadioButton)child.findViewById(R.id.ht4_quality_xueyu_1);
        rb_quality_xueyu_2 = (RadioButton)child.findViewById(R.id.ht4_quality_xueyu_2);
        rb_quality_qiyu_1 = (RadioButton)child.findViewById(R.id.ht4_quality_qiyu_1);
        rb_quality_qiyu_2 = (RadioButton)child.findViewById(R.id.ht4_quality_qiyu_2);
        rb_quality_tebing_1 = (RadioButton)child.findViewById(R.id.ht4_quality_tebing_1);
        rb_quality_tebing_2 = (RadioButton)child.findViewById(R.id.ht4_quality_tebing_2);
        rgHbsab = (RadioGroup)child.findViewById(R.id.hbsab);
        rgChestX = (RadioGroup)child.findViewById(R.id.chestx);
        rgBMode = (RadioGroup)child.findViewById(R.id.bmode);
        rgCervicalSmear = (RadioGroup)child.findViewById(R.id.cervicalsmear);
        rgQualityPinghe = (RadioGroup)child.findViewById(R.id.pinghe);
        rgQualityQixu = (RadioGroup)child.findViewById(R.id.qixu);
        rgQualityYangxu = (RadioGroup)child.findViewById(R.id.yangxu);
        rgQualityYinxu = (RadioGroup)child.findViewById(R.id.yinxu);
        rgQualityTanshi = (RadioGroup)child.findViewById(R.id.tanshi);
        rgQualityShire = (RadioGroup)child.findViewById(R.id.shire);
        rgQualityXueyu = (RadioGroup)child.findViewById(R.id.xueyu);
        rgQualityQiyu = (RadioGroup)child.findViewById(R.id.qiyu);
        rgQualityTebing = (RadioGroup)child.findViewById(R.id.tebing);
	}
	
	/**
	 * 根据ID从数据库中读出数据并填充views
	 */
	public void initViews() {
		// TODO Auto-generated method stub
		
		initialState();
		
		String temp = "";
		int itemp = -1;
		float ftemp = -1f;
		
		itemp = ht.getHete_hbsab();
		switch(itemp){
		case 1:
			rgHbsab.check(0);
			rb_hbsab_1.setChecked(true);
			break;
		case 2:
			rgHbsab.check(1);
			rb_hbsab_2.setChecked(true);
			break;
		}
		
		ftemp = ht.getHete_serum_cpt();
		if(ftemp!=-1f){
			et_serum_cpt.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_serum_ast();
		if(ftemp!=-1f){
			et_serum_ast.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_albumin();
		if(ftemp!=-1f){
			et_albumin.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_tbil();
		if(ftemp!=-1f){
			et_tbil.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_dbil();
		if(ftemp!=-1f){
			et_dbil.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_serum_creatinine();
		if(ftemp!=-1f){
			et_serum_creatinine.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_bun();
		if(ftemp!=-1f){
			et_bun.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_blood_k_chroma();
		if(ftemp!=-1f){
			et_blood_k_chroma.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_blood_na_chroma();
		if(ftemp!=-1f){
			et_blood_na_chroma.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_tc();
		if(ftemp!=-1f){
			et_tc.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_trig();
		if(ftemp!=-1f){
			et_trig.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_serum_ldlc();
		if(ftemp!=-1f){
			et_serum_ldlc.setText(String.valueOf(ftemp));
		}
		ftemp = ht.getHete_serum_hdlc();
		if(ftemp!=-1f){
			et_serum_hdlc.setText(String.valueOf(ftemp));
		}
		
		itemp = ht.getHete_chest_x();
		switch(itemp){
		case 1:
			rgChestX.check(0);
			rb_chest_x_1.setChecked(true);
			break;
		case 2:
			rgChestX.check(1);
			rb_chest_x_2.setChecked(true);
			temp = ht.getHete_chest_x_abnormal();
			et_chest_x.setText(temp);
			break;
		}
		itemp = ht.getHete_brightness_mode();
		switch(itemp){
		case 1:
			rgBMode.check(0);
			rb_b_mode_1.setChecked(true);
			break;
		case 2:
			rgBMode.check(1);
			rb_b_mode_2.setChecked(true);
			temp = ht.getHete_brightness_mode_abnormal();
			et_b_mode.setText(temp);
			break;
		}
		itemp = ht.getHete_cervical_smear();
		switch(itemp){
		case 1:
			rgCervicalSmear.check(0);
			rb_cervical_smear_1.setChecked(true);
			break;
		case 2:
			rgCervicalSmear.check(1);
			rb_cervical_smear_2.setChecked(true);
			temp = ht.getHete_cervical_smear_abnormal();
			et_cervical_smear.setText(temp);
			break;
		}
		
		temp = ht.getHete_assitest_other();
		et_assitest_other.setText(temp);
		
		itemp = ht.getHete_quality_pinghe();
		switch(itemp){
		case 1:
			rgQualityPinghe.check(0);
			rb_quality_pinghe_1.setChecked(true);
			break;
		case 2:
			rgQualityPinghe.check(1);
			rb_quality_pinghe_2.setChecked(true);
			break;
		}
		
		itemp = ht.getHete_quality_qixu();
		switch(itemp){
		case 1:
			rgQualityQixu.check(0);
			rb_quality_qixu_1.setChecked(true);
			break;
		case 2:
			rgQualityQixu.check(1);
			rb_quality_qixu_2.setChecked(true);
			break;
		}
		itemp = ht.getHete_quality_yangxu();
		switch(itemp){
		case 1:
			rgQualityYangxu.check(0);
			rb_quality_yangxu_1.setChecked(true);
			break;
		case 2:
			rgQualityYangxu.check(1);
			rb_quality_yangxu_2.setChecked(true);
			break;
		}
		itemp = ht.getHete_quality_yinxu();
		switch(itemp){
		case 1:
			rgQualityYinxu.check(0);
			rb_quality_yinxu_1.setChecked(true);
			break;
		case 2:
			rgQualityYinxu.check(1);
			rb_quality_yinxu_2.setChecked(true);
			break;
		}
		itemp = ht.getHete_quality_tanshi();
		switch(itemp){
		case 1:
			rgQualityTanshi.check(0);
			rb_quality_tanshi_1.setChecked(true);
			break;
		case 2:
			rgQualityTanshi.check(1);
			rb_quality_tanshi_2.setChecked(true);
			break;
		}
		itemp = ht.getHete_quality_shire();
		switch(itemp){
		case 1:
			rgQualityShire.check(0);
			rb_quality_shire_1.setChecked(true);
			break;
		case 2:
			rgQualityShire.check(1);
			rb_quality_shire_2.setChecked(true);
			break;
		}
		itemp = ht.getHete_quality_xueyu();
		switch(itemp){
		case 1:
			rgQualityXueyu.check(0);
			rb_quality_xueyu_1.setChecked(true);
			break;
		case 2:
			rgQualityXueyu.check(1);
			rb_quality_xueyu_2.setChecked(true);
			break;
		}
		itemp = ht.getHete_quality_qiyu();
		switch(itemp){
		case 1:
			rgQualityQiyu.check(0);
			rb_quality_qiyu_1.setChecked(true);
			break;
		case 2:
			rgQualityQiyu.check(1);
			rb_quality_qiyu_2.setChecked(true);
			break;
		}
		itemp = ht.getHete_quality_tebing();
		switch(itemp){
		case 1:
			rgQualityTebing.check(0);
			rb_quality_tebing_1.setChecked(true);
			break;
		case 2:
			rgQualityTebing.check(1);
			rb_quality_tebing_2.setChecked(true);
			break;
		}
		
	}
	
	/**
	 * 初始时为空表：横线为空，所有选择项未选中
	 */
	private void initialState() {
		// TODO Auto-generated method stub
		et_serum_cpt.setText("");
		et_serum_ast.setText("");
		et_albumin.setText("");
		et_tbil.setText("");
		et_dbil.setText("");
		et_serum_creatinine.setText("");
		et_bun.setText("");
		et_blood_k_chroma.setText("");
		et_blood_na_chroma.setText("");
        et_tc.setText("");
        et_trig.setText("");
        et_serum_ldlc.setText("");
        et_serum_hdlc.setText("");
        et_chest_x.setText("");
        et_b_mode.setText("");
        et_cervical_smear.setText("");
        et_assitest_other.setText("");
		rb_hbsab_1.setChecked(false);
		rb_hbsab_2.setChecked(false);
		rb_chest_x_1.setChecked(false);
		rb_chest_x_2.setChecked(false);
		rb_b_mode_1.setChecked(false);
		rb_b_mode_2.setChecked(false);
		rb_cervical_smear_1.setChecked(false);
		rb_cervical_smear_2.setChecked(false);
        rb_quality_pinghe_1.setChecked(false);
        rb_quality_pinghe_2.setChecked(false);
        rb_quality_qixu_1.setChecked(false);
        rb_quality_qixu_2.setChecked(false);
        rb_quality_yangxu_1.setChecked(false);
        rb_quality_yangxu_2.setChecked(false);
        rb_quality_yinxu_1.setChecked(false);
        rb_quality_yinxu_2.setChecked(false);
        rb_quality_tanshi_1.setChecked(false);
        rb_quality_tanshi_2.setChecked(false);
        rb_quality_shire_1.setChecked(false);
        rb_quality_shire_2.setChecked(false);
        rb_quality_xueyu_1.setChecked(false);
        rb_quality_xueyu_2.setChecked(false);
        rb_quality_qiyu_1.setChecked(false);
        rb_quality_qiyu_2.setChecked(false);
        rb_quality_tebing_1.setChecked(false);
        rb_quality_tebing_2.setChecked(false);
		
	}

	/**
	 * 添加view到contentview
	 */
	public void setMyView(){
		this.removeAllViews();
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		this.addView(child,lp);	
	}
	
	/**
	 * 将整个界面作为一个view对象
	 * @return 整个界面view
	 */
	public View getMyView(){
		return this.child;
	}
	
	/**
	 * 检出数据
	 */
	public void fetchData(){
        ht = new HealthTest(mContext);
		ht.selectHealthTest(this.profileid,this.testid);
		
	}
	
	
	/**
	 * 读取views的值并保存至ID对应的记录中
	 */
	public void saveViews() {
		HealthTest ht = new HealthTest(mContext);
		ht.selectHealthTest(profileid, testid);
		String temp = "";
//		int itemp = -1;
		float ftemp = -1f;
		
		if(rb_hbsab_1.isChecked()==true){
			ht.setHete_hbsab(1);
		}else if(rb_hbsab_2.isChecked()==true){
			ht.setHete_hbsab(2);
		}
		
		temp = et_serum_cpt.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_serum_cpt(ftemp);
		}
		temp = et_serum_ast.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_serum_ast(ftemp);
		}
		temp = et_albumin.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_albumin(ftemp);
		}
		temp = et_tbil.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_tbil(ftemp);
		}
		temp = et_dbil.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_dbil(ftemp);
		}
		temp = et_serum_creatinine.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_serum_creatinine(ftemp);
		}
		temp = et_bun.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_bun(ftemp);
		}
		temp = et_blood_k_chroma.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_blood_k_chroma(ftemp);
		}
		temp = et_blood_na_chroma.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_blood_na_chroma(ftemp);
		}
		temp = et_tc.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_tc(ftemp);
		}
		temp = et_trig.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_trig(ftemp);
		}
		temp = et_serum_ldlc.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_serum_ldlc(ftemp);
		}
		temp = et_serum_hdlc.getText().toString();
		if(temp.length()>0){
			ftemp = Float.parseFloat(temp);
			ht.setHete_serum_hdlc(ftemp);
		}
		
		if(rb_chest_x_1.isChecked()==true){
			ht.setHete_chest_x(1);
		}else if(rb_chest_x_2.isChecked()==true){
			ht.setHete_chest_x(2);
			temp = et_chest_x.getText().toString();
			ht.setHete_chest_x_abnormal(temp);
		}
		if(rb_b_mode_1.isChecked()==true){
			ht.setHete_brightness_mode(1);
		}else if(rb_b_mode_2.isChecked()==true){
			ht.setHete_brightness_mode(2);
			temp = et_b_mode.getText().toString();
			ht.setHete_brightness_mode_abnormal(temp);
		}
		if(rb_cervical_smear_1.isChecked()==true){
			ht.setHete_cervical_smear(1);
		}else if(rb_cervical_smear_2.isChecked()==true){
			ht.setHete_cervical_smear(2);
			temp = et_cervical_smear.getText().toString();
			ht.setHete_cervical_smear_abnormal(temp);
		}
		
		temp = et_assitest_other.getText().toString();
		ht.setHete_assitest_other(temp);
		
		if(rb_quality_pinghe_1.isChecked()==true){
			ht.setHete_quality_pinghe(1);
		}else if(rb_quality_pinghe_2.isChecked()==true){
			ht.setHete_quality_pinghe(2);
		}
		if(rb_quality_qixu_1.isChecked()==true){
			ht.setHete_quality_qixu(1);
		}else if(rb_quality_qixu_2.isChecked()==true){
			ht.setHete_quality_qixu(2);
		}
		if(rb_quality_yangxu_1.isChecked()==true){
			ht.setHete_quality_yangxu(1);
		}else if(rb_quality_yangxu_2.isChecked()==true){
			ht.setHete_quality_yangxu(2);
		}
		if(rb_quality_yinxu_1.isChecked()==true){
			ht.setHete_quality_yinxu(1);
		}else if(rb_quality_yinxu_2.isChecked()==true){
			ht.setHete_quality_yinxu(2);
		}
		if(rb_quality_tanshi_1.isChecked()==true){
			ht.setHete_quality_tanshi(1);
		}else if(rb_quality_tanshi_2.isChecked()==true){
			ht.setHete_quality_tanshi(2);
		}
		if(rb_quality_shire_1.isChecked()==true){
			ht.setHete_quality_shire(1);
		}else if(rb_quality_shire_2.isChecked()==true){
			ht.setHete_quality_shire(2);
		}
		if(rb_quality_xueyu_1.isChecked()==true){
			ht.setHete_quality_xueyu(1);
		}else if(rb_quality_xueyu_2.isChecked()==true){
			ht.setHete_quality_xueyu(2);
		}
		if(rb_quality_qiyu_1.isChecked()==true){
			ht.setHete_quality_qiyu(1);
		}else if(rb_quality_qiyu_2.isChecked()==true){
			ht.setHete_quality_qiyu(2);
		}
		if(rb_quality_tebing_1.isChecked()==true){
			ht.setHete_quality_tebing(1);
		}else if(rb_quality_tebing_2.isChecked()==true){
			ht.setHete_quality_tebing(2);
		}
		
		ht.updateHealthTest(this.profileid,this.testid);
		
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==et_chest_x){
			rb_chest_x_1.setChecked(false);
			rb_chest_x_2.setChecked(true);
		}
		else if(v==et_b_mode){
			rb_b_mode_1.setChecked(false);
			rb_b_mode_2.setChecked(true);
		}
		else if(v==et_cervical_smear){
			rb_cervical_smear_1.setChecked(false);
			rb_cervical_smear_2.setChecked(true);
		}
		
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		if(hasFocus){
			if(v==et_chest_x){
				rb_chest_x_1.setChecked(false);
				rb_chest_x_2.setChecked(true);
			}
			else if(v==et_b_mode){
				rb_b_mode_1.setChecked(false);
				rb_b_mode_2.setChecked(true);
			}
			else if(v==et_cervical_smear){
				rb_cervical_smear_1.setChecked(false);
				rb_cervical_smear_2.setChecked(true);
			}
		}
	}

}
